<template>
  <div class="form">
    <!-- {{ form }} -->
    <a-form ref="formRef" :layout="layout" :model="form">
      <a-form-item
        v-for="(item, v) in formList"
        :key="v"
        :label="item.label"
        :name="item.key"
        :rules="item.rules"
        :labelCol="layout == 'inline' ? {} : labelCol"
        :wrapperCol="layout == 'inline' ? {} : wrapperCol"
      >
        <!-- 开关 -->
        <a-switch
          v-if="item.type == 'switch'"
          v-model:checked="form[item.key]"
          :checkedValue="item.checkedValue || true"
          :unCheckedValue="item.unCheckedValue || false"
        />
        <!-- 密码输入框 -->
        <a-input
          v-else-if="item.type == 'password'"
          type="password"
          v-model:value="form[item.key]"
          :placeholder="item.placeholder"
        />
        <!-- 图标选择器 -->
        <SelectIcon v-else-if="item.type == 'icon'" v-model:value="form[item.key]" />
        <!-- 日期 -->
        <DatePicker v-else-if="item.type == 'date'" v-model:value="form[item.key]" />
        <RangePicker
          v-else-if="item.type == 'dateRange'"
          v-model:value="form[item.key]"
          :showTime="item.showTime"
        />
        <!-- 字典 -->
        <SelectDict
          v-else-if="item.type == 'dict'"
          :dictId="item.dictId"
          v-model:value="form[item.key]"
        />
        <!-- 插槽 -->
        <slot v-else-if="item.type == 'slot'" :name="item.key"></slot>
        <!-- 输入框 -->
        <a-input v-else v-model:value="form[item.key]" :placeholder="item.placeholder" />
      </a-form-item>

      <a-form-item v-if="btnConfig != null">
        <a-space>
          <a-button type="primary" @click="onSubmit" v-if="btnConfig.confirmBtn">
            <template #icon v-if="btnConfig.confirmBtnIconStyle == 1">
              <SearchOutlined />
            </template>
            <span>{{ btnConfig.confirmBtnName }}</span>
          </a-button>
          <a-button style="margin-left: 10px" v-if="btnConfig.cancelBtn" @click="cancel">
            <template #icon v-if="btnConfig.cancelBtnIconStyle == 1">
              <UndoOutlined />
            </template>
            <span>{{ btnConfig.cancelBtnName }}</span>
          </a-button>
        </a-space>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, toRefs, defineProps, defineExpose, defineEmits } from "vue";
import SelectIcon from "@/components/SelectIcon";
import DatePicker from "@/components/SelectDate/DatePicker";
import RangePicker from "@/components/SelectDate/RangePicker";
import SelectDict from "@/components/SelectDict";

const emits = defineEmits(["save"]);
const props = defineProps({
  drakTheme: Boolean,
  form: Object,
  formList: Object,
  layout: {
    type: String,
    default: "horizontal", //inline
  },
  labelCol: {
    default: { span: 6 },
  },
  wrapperCol: {
    default: { span: 14, offset: 1 },
  },
  btnConfig: {
    default: {
      confirmBtn: true,
      confirmBtnName: "保存",
      confirmBtnIconStyle: 1,
      cancelBtn: true,
      cancelBtnName: "重置",
      cancelBtnIconStyle: 1,
    },
  },
});
const { drakTheme, layout, labelCol, wrapperCol, form, formList, btnConfig } = toRefs(
  props
);
const formRef = ref();

async function save() {
  return await formRef.value.validate();
}
async function onSubmit() {
  await formRef.value.validate();
  emits("save", true);
}
async function cancel() {
  await formRef.value.resetFields();
  emits("save", false);
}

defineExpose({
  save,
});
</script>

<style lang="less" scoped>
/deep/ .ant-form-inline .ant-form-item {
  margin-bottom: 10px;
}
/deep/ .ant-form-item-control {
  min-width: 180px;
  // min-width: 40px;
}
</style>
